/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package frame.Report;

import java.awt.BorderLayout;
import java.awt.Container;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import myclass.MyInterFrame;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.swing.JRViewer;

/**
 *
 * @author Quan
 */
public class ReportFrame extends MyInterFrame {
    
    private Container container;

    /**
     * Creates new form ReportSanPham
     */
    public ReportFrame() {
        initComponents();
        this.container = this.getContentPane();
        setDefaultCloseOperation(HIDE_ON_CLOSE);
    }
    
    public void LoadReport(String fileName) throws SQLException, JRException {
        this.RemoveCurrentReport();
        try {
            Class.forName("org.postgresql.Driver");
            Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/qlchuoidiendandung", "postgres", "q");
            
            HashMap hm = new HashMap();
            JasperPrint print = JasperFillManager.fillReport(fileName, hm, con);
            JRViewer viewer = new JRViewer(print);
            Container c = getContentPane();
            c.setLayout(new BorderLayout());
            c.add(viewer);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ReportFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void LoadReport(String title, String fileName, HashMap hm) throws SQLException, JRException {
        this.setTitle(title);
        this.RemoveCurrentReport();
        try {
            Class.forName("org.postgresql.Driver");
            Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/qlchuoidiendandung", "postgres", "q");
            JasperPrint print = JasperFillManager.fillReport(fileName, hm, con);
            JRViewer viewer = new JRViewer(print);
            Container c = getContentPane();
            c.setLayout(new BorderLayout());
            c.add(viewer);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ReportFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    private void RemoveCurrentReport() {
        this.container.removeAll();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setIconifiable(true);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 473, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 342, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
